//
//  GYShopDetailsViewController.m
//  GoodLuck
//
//  Created by apple on 2018/2/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *goodsDetailesCellIdentifier = @"goodsDetailesCellIdentifier";

#import "GYShopDetailsViewController.h"
#import "GYNetWorkTools.h"
#import "GYNoAddressView.h"
#import "GYShopDetailes.h"
#import "GYGoodsDetailesCollectionViewCell.h"
#import <SDWebImage/SDImageCache.h>

@interface GYShopDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/** 商品详情数据源 */
@property (nonatomic, strong) NSArray *detaileList;

/** <#description#> */
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation GYShopDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(245, 245, 245);
    
    [self setupCollectionView];
    //初始化图文详情数据
    [self laodShopImageDetails];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [EasyLodingView hidenLoding];
}
- (void)setupCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GYGoodsDetailesCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:goodsDetailesCellIdentifier];
}

- (void)laodShopImageDetails {
    [GYShopDetailes loadGoodsDetailsData:self.goods_id finish:^(NSArray * _Nullable array, NSError * _Nullable error) {
        if (array != nil) {
            self.detaileList = array;
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark -- uicolelctionDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.detaileList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodsDetailesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:goodsDetailesCellIdentifier forIndexPath:indexPath];
    cell.model = self.detaileList[indexPath.item];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYShopDetailes *model = self.detaileList[indexPath.item];
    if ([model.type isEqualToString:@"image"]) {
        //计算cell的高度
        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.value];
        if (image) {
            return CGSizeMake(ScreenWidth, ScreenWidth*image.size.height/image.size.width);
        }else {
            //1.比如图片下载失败之后,无法获取图片的真实高度  暂时用300 个高度来替代;
            return CGSizeMake(ScreenWidth, 300);
        }
    }else {
        CGFloat height = [model.value boundingRectWithSize:CGSizeMake(ScreenWidth-20, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
        return CGSizeMake(ScreenWidth, height);
    }
}



- (void)dealloc {
    NSLog(@"GYShopDetailsViewController===dealloc");
}
@end
